#!/usr/bin/python3

import psutil

def menu():
    print("*******************")
    print('''
    1.内存使用情况
    2.CPU使用情况
    3.硬盘使用情况
    4.网络流量
    5.退出
    ''')
    print("******************")

def memory_info():
    mem = psutil.virtual_memory()
    free = int(mem.free/1024/1024)
    total = int(mem.total/1024/1024)
    used = total - free
    print("total : %s M" %total)
    print("free : %s M" %free)
    print("used : %s M" %used)
    a = int(used*100/total)
    if a > 80:
        print("%s 内存大于80，请处理"% a)
    else:
        print("内存使用率为: {0}% ".format(a))


#2.CPU使用情况def cpu_info():
    cpu = psutil.cpu_times_percent()
    a = cpu.user
    print("CPU 用户使用：%s "%a)
    b = cpu.system
    print("CPU 系统使用: %s"%b)

def disk_info():
    disk_all = psutil.disk_usage('/')
    disk_total = int(disk_all.total/1024/1024)
    disk_used = int(disk_all.used/1024/1024)
    disk_free = int(disk_all.free/1024/1024)
    print("disk_total is %s M" % disk_total)
    print("disk_used is %s M" % disk_used)
    print("disk_free is %s M" % disk_free)

def net_info():
    net = psutil.net_io_counters()
    recv = int(net.bytes_recv/1024/1024)
    sent = int(net.bytes_sent/1024/1024)
    packe_sent = net.packets_sent
    packe_recv = net.packets_recv
    print("接收 ：%s M" %recv)
    print("发送 ：%s M" %sent)
    print("发送数据包：%s" %packe_sent)
    print("接收数据包：%s" %packe_recv)


opt = {"1":memory_info,"2":cpu_info,"3":disk_info,"4":net_info,"5":exit}

def main():
    menu()
    u_opt=input("请输入你的选择：")
    if u_opt in opt:
        opt.get(u_opt)()
    else:
        print("请输入合理的参数")
while True:
    main()